package org.demo.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件工具类
 * @author  
 * @date    2011-3-11
 * @file    org.demo.file.FileUtils.java
 */
public class FileUtils {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String src = "D:/workspace/eclipse_inspur/adapter_config_pro/WebContent/WEB-INF/ra/I_SPYW_APPLIANCE_INVIREAGENT/I_SPYW_APPLIANCE_INVIREAGENTBG.xsd";
		String dest = "D:/workspace/eclipse_inspur/adapter_config_pro/WebContent/WEB-INF/ra/target_databases/pre_accept.xsd";
		String path = getRelativePath(new File(src), new File(dest));
		System.out.println(path);
	}
	/**
	 * 计算文件 target 相对于文件 base 的相对路径
	 * @param base
	 * @param target
	 * @return
	 */
	public static String getRelativePath(File base, File target){
		
		StringBuilder path = new StringBuilder();
		if (base.isFile()){
			base = base.getParentFile();
		}
		
		String base_path = base.getAbsolutePath();
		String target_path = target.getAbsolutePath();
		while (!target_path.startsWith(base_path)){
			path.append("../");
			base = base.getParentFile();
			if (base == null){
				path.delete(0, path.length());
				break;
			}
			base_path = base.getAbsolutePath();
		}	
		if (base != null){
			path.append(target_path.substring(base_path.length() + 1));
		} else {
			path.append(target_path);
		}
		
		return path.toString();
	}
	
	/**
     * 返回目录 dir 及所有子目录中以 suffix 结尾的 文件
     * @param dir :String
     * @param suffix :String
     * @return List<String> fileAbsolutePath
     */
    public static List<String> getAllFiles(String dir,String suffix){
        List<String> list = new ArrayList<String>();
        File file = new File(dir);
        String filename = file.getName().toLowerCase(); // 文件名转小写
        if(file.isFile() && (filename.endsWith(suffix))){
            list.add(file.getAbsolutePath());           // 保存符合条件的文件路径
        } else if(file.isDirectory()){
            File[] fs = file.listFiles();
            for(File f : fs){                           // 遍历当前目录
                getAllFiles(f,suffix,list);             // 递归遍历子目录
            }
        }
        return list;
    } 
    /**
     * 取出目录 dir 及所有子目录中以 suffix 结尾的 文件
     * @param file :File
     * @param suffix :String
     * @param list :List<String>
     */
    private static void getAllFiles(File file,String suffix,List<String> list){
        String filename = file.getName().toLowerCase(); // 文件名转小写
        if(file.isFile() && (filename.endsWith(suffix))){
            list.add(file.getAbsolutePath());           // 保存符合条件的文件路径
        } else if(file.isDirectory()){
            File[] fs = file.listFiles();
            for(File f : fs){                           // 遍历当前目录
                getAllFiles(f,suffix,list);             // 递归遍历子目录
            }
        }
    }// end
    
    /**
     * 加密文件[解密文件时重新执行一遍即可]
     * @param filePath
     * @param pass
     * @throws IOException 
     */
    public static void encrypt(String filePath, int pass) throws IOException{
    	File src = new File(filePath);
    	File dest = new File(filePath + ".tmp");
    	FileInputStream fis = new FileInputStream(src);
    	FileOutputStream fos = new FileOutputStream(dest);
    	
    	byte[] data = new byte[8192];
    	int read = -1;
    	
    	while ((read = fis.read(data)) != -1){
    		for (int i=0; i<read; i++){
    			data[i] = (byte)(data[i] ^ pass);
    		}
    		fos.write(data, 0, read);
    		fos.flush();
    	}
    	
    	fos.close();
    	fis.close();
    	
    	src.delete();
    	dest.renameTo(src);    	
    }
}
